/*
* Copyright (c) 2007 by Kirill Kolodyazhniy.
* See the file "license.terms" for information on usage and redistribution.
*/

#include "cgstatemanager.h"
#include "graphic.h"

namespace Engine
{
	/**********************************************************************************************************/
	CGbool SetStateAssignment(CGstateassignment as);
	CGbool ResetStateAssignment(CGstateassignment as);
	CGbool ValidateStateAssignment(CGstateassignment as);
	/**********************************************************************************************************/
	template<> CGStateManager* Singleton<CGStateManager>::m_singleton = 0;
	/*******************************************************************************/
	CGStateManager::CGStateManager()
	{
		m_cgcontext = cgCreateContext();

		CGstate newstate = cgCreateState(m_cgcontext,"ambient", CG_FLOAT4);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"diffuse", CG_FLOAT4);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"specular", CG_FLOAT4);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"emissive", CG_FLOAT4);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"shininess", CG_FLOAT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"depth_check",CG_BOOL);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"depth_write",CG_BOOL);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"depth_bias", CG_FLOAT2);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"depth_func", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "never", static_cast<int>(CMPF_ALWAYS_FAIL));
		cgAddStateEnumerant( newstate, "less", static_cast<int>(CMPF_LESS));
		cgAddStateEnumerant( newstate, "equal", static_cast<int>(CMPF_EQUAL));
		cgAddStateEnumerant( newstate, "lessequal", static_cast<int>(CMPF_LESS_EQUAL));
		cgAddStateEnumerant( newstate, "greater", static_cast<int>(CMPF_GREATER));
		cgAddStateEnumerant( newstate, "notequal", static_cast<int>(CMPF_NOT_EQUAL));
		cgAddStateEnumerant( newstate, "greaterequal", static_cast<int>(CMPF_GREATER_EQUAL));
		cgAddStateEnumerant( newstate, "always", static_cast<int>(CMPF_ALWAYS_PASS));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"shading", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "flat", static_cast<int>(SM_FLAT));
		cgAddStateEnumerant( newstate, "gouraud", static_cast<int>(SM_GOURAUD));
		cgAddStateEnumerant( newstate, "phong", static_cast<int>(SM_PHONG));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"cull_mode", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "none", static_cast<int>(FCM_NONE));
		cgAddStateEnumerant( newstate, "cw", static_cast<int>(FCM_CW));
		cgAddStateEnumerant( newstate, "ccw", static_cast<int>(FCM_CCW));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"polygon_mode", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "points", static_cast<int>(PM_POINTS));
		cgAddStateEnumerant( newstate, "solid", static_cast<int>(PM_SOLID));
		cgAddStateEnumerant( newstate, "wireframe", static_cast<int>(PM_WIREFRAME));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);


		newstate = cgCreateState(m_cgcontext,"lighting", CG_BOOL);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"alpha_rejection", CG_INT2);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "never", static_cast<int>(CMPF_ALWAYS_FAIL));
		cgAddStateEnumerant( newstate, "less", static_cast<int>(CMPF_LESS));
		cgAddStateEnumerant( newstate, "equal", static_cast<int>(CMPF_EQUAL));
		cgAddStateEnumerant( newstate, "lessequal", static_cast<int>(CMPF_LESS_EQUAL));
		cgAddStateEnumerant( newstate, "greater", static_cast<int>(CMPF_GREATER));
		cgAddStateEnumerant( newstate, "notequal", static_cast<int>(CMPF_NOT_EQUAL));
		cgAddStateEnumerant( newstate, "greaterequal", static_cast<int>(CMPF_GREATER_EQUAL));
		cgAddStateEnumerant( newstate, "always", static_cast<int>(CMPF_ALWAYS_PASS));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"scene_blend", CG_INT2);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "zero" , static_cast<int>(SBF_ZERO));
		cgAddStateEnumerant( newstate, "one" , static_cast<int>(SBF_ONE));
		cgAddStateEnumerant( newstate, "srccolor" , static_cast<int>(SBF_SOURCE_COLOUR));
		cgAddStateEnumerant( newstate, "invsrccolor" , static_cast<int>(SBF_ONE_MINUS_SOURCE_COLOUR));
		cgAddStateEnumerant( newstate, "srcalpha" , static_cast<int>(SBF_SOURCE_ALPHA));
		cgAddStateEnumerant( newstate, "invsrcalpha" , static_cast<int>(SBF_ONE_MINUS_SOURCE_ALPHA));
		cgAddStateEnumerant( newstate, "destalpha" , static_cast<int>(SBF_DEST_ALPHA));
		cgAddStateEnumerant( newstate, "invdestalpha" , static_cast<int>(SBF_ONE_MINUS_DEST_ALPHA));
		cgAddStateEnumerant( newstate, "destcolor" , static_cast<int>(SBF_DEST_COLOUR));
		cgAddStateEnumerant( newstate, "invdestcolor" , static_cast<int>(SBF_ONE_MINUS_DEST_COLOUR));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

//fog

		newstate = cgCreateState(m_cgcontext,"fog_mode", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "none" , static_cast<int>(FOG_NONE));
		cgAddStateEnumerant( newstate, "exp" , static_cast<int>(FOG_EXP));
		cgAddStateEnumerant( newstate, "exp2" , static_cast<int>(FOG_EXP2));
		cgAddStateEnumerant( newstate, "linear" , static_cast<int>(FOG_LINEAR));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"fog_color", CG_FLOAT4);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"fog_density", CG_FLOAT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"fog_start", CG_FLOAT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"fog_end", CG_FLOAT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

//point sprites

		newstate = cgCreateState(m_cgcontext,"point_size", CG_FLOAT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"point_sprites", CG_BOOL);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"point_size_attenuation", CG_FLOAT3);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"point_size_min", CG_FLOAT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"point_size_max", CG_FLOAT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

//stencil

		newstate = cgCreateState(m_cgcontext,"stencil", CG_BOOL);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"stencil_func", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "never", static_cast<int>(CMPF_ALWAYS_FAIL));
		cgAddStateEnumerant( newstate, "less", static_cast<int>(CMPF_LESS));
		cgAddStateEnumerant( newstate, "equal", static_cast<int>(CMPF_EQUAL));
		cgAddStateEnumerant( newstate, "lessequal", static_cast<int>(CMPF_LESS_EQUAL));
		cgAddStateEnumerant( newstate, "greater", static_cast<int>(CMPF_GREATER));
		cgAddStateEnumerant( newstate, "notequal", static_cast<int>(CMPF_NOT_EQUAL));
		cgAddStateEnumerant( newstate, "greaterequal", static_cast<int>(CMPF_GREATER_EQUAL));
		cgAddStateEnumerant( newstate, "always", static_cast<int>(CMPF_ALWAYS_PASS));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"stencil_ref_value", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"stencil_mask", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"stencil_fail", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "keep", static_cast<int>(SOP_KEEP));
		cgAddStateEnumerant( newstate, "zero", static_cast<int>(SOP_ZERO));
		cgAddStateEnumerant( newstate, "replace", static_cast<int>(SOP_REPLACE));
		cgAddStateEnumerant( newstate, "inc", static_cast<int>(SOP_INCREMENT));
		cgAddStateEnumerant( newstate, "dec", static_cast<int>(SOP_DECREMENT));
		cgAddStateEnumerant( newstate, "inc_wrap", static_cast<int>(SOP_INCREMENT_WRAP));
		cgAddStateEnumerant( newstate, "dec_wrap", static_cast<int>(SOP_DECREMENT_WRAP));
		cgAddStateEnumerant( newstate, "inv", static_cast<int>(SOP_INVERT));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"stencil_depth_fail", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "keep", static_cast<int>(SOP_KEEP));
		cgAddStateEnumerant( newstate, "zero", static_cast<int>(SOP_ZERO));
		cgAddStateEnumerant( newstate, "replace", static_cast<int>(SOP_REPLACE));
		cgAddStateEnumerant( newstate, "inc", static_cast<int>(SOP_INCREMENT));
		cgAddStateEnumerant( newstate, "dec", static_cast<int>(SOP_DECREMENT));
		cgAddStateEnumerant( newstate, "inc_wrap", static_cast<int>(SOP_INCREMENT_WRAP));
		cgAddStateEnumerant( newstate, "dec_wrap", static_cast<int>(SOP_DECREMENT_WRAP));
		cgAddStateEnumerant( newstate, "inv", static_cast<int>(SOP_INVERT));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateState(m_cgcontext,"stencil_pass", CG_INT);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "keep", static_cast<int>(SOP_KEEP));
		cgAddStateEnumerant( newstate, "zero", static_cast<int>(SOP_ZERO));
		cgAddStateEnumerant( newstate, "replace", static_cast<int>(SOP_REPLACE));
		cgAddStateEnumerant( newstate, "inc", static_cast<int>(SOP_INCREMENT));
		cgAddStateEnumerant( newstate, "dec", static_cast<int>(SOP_DECREMENT));
		cgAddStateEnumerant( newstate, "inc_wrap", static_cast<int>(SOP_INCREMENT_WRAP));
		cgAddStateEnumerant( newstate, "dec_wrap", static_cast<int>(SOP_DECREMENT_WRAP));
		cgAddStateEnumerant( newstate, "inv", static_cast<int>(SOP_INVERT));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

//texture stages

		newstate = cgCreateArrayState(m_cgcontext,"texture", CG_TEXTURE, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"tex_address_mode", CG_INT3, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "wrap" , static_cast<int>(TAM_WRAP));
		cgAddStateEnumerant( newstate, "mirror" , static_cast<int>(TAM_MIRROR));
		cgAddStateEnumerant( newstate, "clamp" , static_cast<int>(TAM_CLAMP));
		cgAddStateEnumerant( newstate, "border" , static_cast<int>(TAM_BORDER));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"tex_border_color", CG_FLOAT4, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"filtering", CG_INT3, 8);
		cgAddStateEnumerant( newstate, "none" , static_cast<int>(FO_NONE));
		cgAddStateEnumerant( newstate, "point" , static_cast<int>(FO_POINT));
        cgAddStateEnumerant( newstate, "linear" , static_cast<int>(FO_LINEAR));
        cgAddStateEnumerant( newstate, "anisotropic" , static_cast<int>(FO_ANISOTROPIC));
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"max_anisotropy", CG_INT, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"mipmap_bias", CG_FLOAT, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"transform", CG_FLOAT4x4, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"transform_dim", CG_INT, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "disable" , static_cast<int>(TCD_DISABLE));
		cgAddStateEnumerant( newstate, "count1" , static_cast<int>(TCD_COUNT1));
		cgAddStateEnumerant( newstate, "count2" , static_cast<int>(TCD_COUNT2));
		cgAddStateEnumerant( newstate, "count3" , static_cast<int>(TCD_COUNT3));
		cgAddStateEnumerant( newstate, "count4" , static_cast<int>(TCD_COUNT4));
		cgAddStateEnumerant( newstate, "projected" , static_cast<int>(TCD_PROJECTED));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"tex_coord_gen", CG_INT2, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "none" , static_cast<int>(TEXCALC_NONE));
        cgAddStateEnumerant( newstate, "env_map" , static_cast<int>(TEXCALC_ENVIRONMENT_MAP));
        cgAddStateEnumerant( newstate, "env_map_planar" , static_cast<int>(TEXCALC_ENVIRONMENT_MAP_PLANAR));
        cgAddStateEnumerant( newstate, "env_map_reflection" , static_cast<int>(TEXCALC_ENVIRONMENT_MAP_REFLECTION));
        cgAddStateEnumerant( newstate, "env_map_normal" , static_cast<int>(TEXCALC_ENVIRONMENT_MAP_NORMAL));
        cgAddStateEnumerant( newstate, "proj_tex" , static_cast<int>(TEXCALC_PROJECTIVE_TEXTURE));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);
//COLOR
		newstate = cgCreateArrayState(m_cgcontext,"color_blend_operation", CG_INT, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "source1" , static_cast<int>(LBX_SOURCE1));
        cgAddStateEnumerant( newstate, "source2" , static_cast<int>(LBX_SOURCE2));
        cgAddStateEnumerant( newstate, "modulate" , static_cast<int>(LBX_MODULATE));
        cgAddStateEnumerant( newstate, "modulate_x2" , static_cast<int>(LBX_MODULATE_X2));
        cgAddStateEnumerant( newstate, "modulate_x4" , static_cast<int>(LBX_MODULATE_X4));
        cgAddStateEnumerant( newstate, "add" , static_cast<int>(LBX_ADD));
        cgAddStateEnumerant( newstate, "add_signed" , static_cast<int>(LBX_ADD_SIGNED));
        cgAddStateEnumerant( newstate, "add_smooth" , static_cast<int>(LBX_ADD_SMOOTH));
        cgAddStateEnumerant( newstate, "subtract" , static_cast<int>(LBX_SUBTRACT));
        cgAddStateEnumerant( newstate, "blend_diffuse_alpha" , static_cast<int>(LBX_BLEND_DIFFUSE_ALPHA));
        cgAddStateEnumerant( newstate, "blend_texture_alpha" , static_cast<int>(LBX_BLEND_TEXTURE_ALPHA));
        cgAddStateEnumerant( newstate, "blend_current_alpha" , static_cast<int>(LBX_BLEND_CURRENT_ALPHA));
        cgAddStateEnumerant( newstate, "blend_manual" , static_cast<int>(LBX_BLEND_MANUAL));
        cgAddStateEnumerant( newstate, "dotproduct" , static_cast<int>(LBX_DOTPRODUCT));
        cgAddStateEnumerant( newstate, "blend_diffuse_color" , static_cast<int>(LBX_BLEND_DIFFUSE_COLOUR));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"color_blend_sources", CG_INT2, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "current" , static_cast<int>(LBS_CURRENT));
        cgAddStateEnumerant( newstate, "texturesrc" , static_cast<int>(LBS_TEXTURE));
        cgAddStateEnumerant( newstate, "diffuse" , static_cast<int>(LBS_DIFFUSE));
        cgAddStateEnumerant( newstate, "specular" , static_cast<int>(LBS_SPECULAR));
        cgAddStateEnumerant( newstate, "manual" , static_cast<int>(LBS_MANUAL));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"color_blend_factor", CG_FLOAT, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"color_blend_color1", CG_FLOAT4, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"color_blend_color2", CG_FLOAT4, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);
//ALPHA
		newstate = cgCreateArrayState(m_cgcontext,"alpha_blend_operation", CG_INT, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "source1" , static_cast<int>(LBX_SOURCE1));
        cgAddStateEnumerant( newstate, "source2" , static_cast<int>(LBX_SOURCE2));
        cgAddStateEnumerant( newstate, "modulate" , static_cast<int>(LBX_MODULATE));
        cgAddStateEnumerant( newstate, "modulate_x2" , static_cast<int>(LBX_MODULATE_X2));
        cgAddStateEnumerant( newstate, "modulate_x4" , static_cast<int>(LBX_MODULATE_X4));
        cgAddStateEnumerant( newstate, "add" , static_cast<int>(LBX_ADD));
        cgAddStateEnumerant( newstate, "add_signed" , static_cast<int>(LBX_ADD_SIGNED));
        cgAddStateEnumerant( newstate, "add_smooth" , static_cast<int>(LBX_ADD_SMOOTH));
        cgAddStateEnumerant( newstate, "subtract" , static_cast<int>(LBX_SUBTRACT));
        cgAddStateEnumerant( newstate, "blend_diffuse_alpha" , static_cast<int>(LBX_BLEND_DIFFUSE_ALPHA));
        cgAddStateEnumerant( newstate, "blend_texture_alpha" , static_cast<int>(LBX_BLEND_TEXTURE_ALPHA));
        cgAddStateEnumerant( newstate, "blend_current_alpha" , static_cast<int>(LBX_BLEND_CURRENT_ALPHA));
        cgAddStateEnumerant( newstate, "blend_manual" , static_cast<int>(LBX_BLEND_MANUAL));
        cgAddStateEnumerant( newstate, "dotproduct" , static_cast<int>(LBX_DOTPRODUCT));
        cgAddStateEnumerant( newstate, "blend_diffuse_color" , static_cast<int>(LBX_BLEND_DIFFUSE_COLOUR));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"alpha_blend_sources", CG_INT2, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgAddStateEnumerant( newstate, "current" , static_cast<int>(LBS_CURRENT));
        cgAddStateEnumerant( newstate, "texturesrc" , static_cast<int>(LBS_TEXTURE));
        cgAddStateEnumerant( newstate, "diffuse" , static_cast<int>(LBS_DIFFUSE));
        cgAddStateEnumerant( newstate, "specular" , static_cast<int>(LBS_SPECULAR));
        cgAddStateEnumerant( newstate, "manual" , static_cast<int>(LBS_MANUAL));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"alpha_blend_factor", CG_FLOAT, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"alpha_blend_color1", CG_FLOAT4, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

		newstate = cgCreateArrayState(m_cgcontext,"alpha_blend_color2", CG_FLOAT4, 8);
		Assert(m_cgcontext == cgGetStateContext(newstate));
		cgSetStateCallbacks(newstate,&SetStateAssignment,&ResetStateAssignment,&ValidateStateAssignment);

	}
	/*******************************************************************************/
	CGStateManager::~CGStateManager()
	{
		if(m_cgcontext)
		{
			cgDestroyContext(m_cgcontext);
			m_cgcontext = 0;
		}
	}
	/*******************************************************************************/
	CGPassData& CGStateManager::GetPassData(CGpass pass)
	{
		return m_passData[pass];
	}
	/*******************************************************************************/
	CGbool SetStateAssignment(CGstateassignment as)
	{
		CGstate state = cgGetStateAssignmentState( as );
		if(cgIsState(state))
		{
			const char* name = cgGetStateName( state );
			CGpass pass = cgGetStateAssignmentPass( as );
			int nvalues = 0;
			if(std::string("ambient") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).ambient = Color(val[0],val[1],val[2],val[3]);
				return CG_TRUE;
			}
			else if(std::string("diffuse") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).diffuse = Color(val[0],val[1],val[2],val[3]);
				return CG_TRUE;
			}
			else if(std::string("specular") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).specular = Color(val[0],val[1],val[2],val[3]);
				return CG_TRUE;
			}
			else if(std::string("emissive") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).emissive = Color(val[0],val[1],val[2],val[3]);
				return CG_TRUE;
			}
			else if(std::string("shininess") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).shininess = val[0];
				return CG_TRUE;
			}
			else if(std::string("depth_check") ==  name)
			{
				const CGbool* val = cgGetBoolStateAssignmentValues(as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).depth_check = val[0] == 1;
				return CG_TRUE;
			}
			else if(std::string("depth_write") ==  name)
			{
				const CGbool* val = cgGetBoolStateAssignmentValues(as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).depth_write = val[0] == 1;
				return CG_TRUE;
			}
			else if(std::string("depth_bias") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).depth_bias[0] = val[0];
				CGStateManager::GetSingleton().GetPassData(pass).depth_bias[1] = val[1];
				return CG_TRUE;
			}
			else if(std::string("depth_func") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).depth_func = static_cast<CompareFunction>(val[0]);
				return CG_TRUE;
			}
			else if(std::string("shading") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).shading = static_cast<ShadeMode>(val[0]);
				return CG_TRUE;
			}
			else if(std::string("cull_mode") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).cull_mode = static_cast<FaceCullMode>(val[0]);
				return CG_TRUE;
			}
			else if(std::string("lighting") ==  name)
			{
				const CGbool* val = cgGetBoolStateAssignmentValues(as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).lighting = val[0] == 1;
				return CG_TRUE;
			}
			else if(std::string("polygon_mode") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).polygon_mode = static_cast<PolygonMode>(val[0]);
				return CG_TRUE;
			}
			else if(std::string("alpha_rejection") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).alpha_rejection_func = static_cast<CompareFunction>(val[0]);
				CGStateManager::GetSingleton().GetPassData(pass).alpha_rejection_val = static_cast<unsigned char>(val[1]);
				return CG_TRUE;
			}
			else if(std::string("scene_blend") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).scene_blend[0] = static_cast<SceneBlendFactor>(val[0]);
				CGStateManager::GetSingleton().GetPassData(pass).scene_blend[1] = static_cast<SceneBlendFactor>(val[1]);
				return CG_TRUE;
			}
			else if(std::string("fog_mode") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).fog_mode = static_cast<FogMode>(val[0]);
				return CG_TRUE;
			}
			else if(std::string("fog_color") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).fog_color = Color(val[0],val[1],val[2],val[3]);
				return CG_TRUE;
			}
			else if(std::string("fog_density") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).fog_density = val[0];
				return CG_TRUE;
			}
			else if(std::string("fog_start") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).fog_start = val[0];
				return CG_TRUE;
			}
			else if(std::string("fog_end") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).fog_end = val[0];
				return CG_TRUE;
			}
			else if(std::string("point_size") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).point_size = val[0];
				return CG_TRUE;
			}
			else if(std::string("point_sprites") ==  name)
			{
				const CGbool* val =  cgGetBoolStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).point_sprites = val[0] == 1;
				return CG_TRUE;
			}
			else if(std::string("point_size_attenuation") ==  name)
			{
				CGStateManager::GetSingleton().GetPassData(pass).point_size_attenuation_enabled = true;
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).point_size_attenuation[0] = val[0];
				CGStateManager::GetSingleton().GetPassData(pass).point_size_attenuation[1] = val[1];
				CGStateManager::GetSingleton().GetPassData(pass).point_size_attenuation[2] = val[2];
				return CG_TRUE;
			}
			else if(std::string("point_size_min") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).point_size_min = val[0];
				return CG_TRUE;
			}
			else if(std::string("point_size_max") ==  name)
			{
				const float* val =  cgGetFloatStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).point_size_max = val[0];
				return CG_TRUE;
			}
//stencil
			else if(std::string("stencil") ==  name)
			{
				const CGbool* val =  cgGetBoolStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).stencil = val[0] == 1;
				return CG_TRUE;
			}
			else if(std::string("stencil_func") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).stencil_func = static_cast<CompareFunction>(val[0]);
				return CG_TRUE;
			}
			else if(std::string("stencil_ref_value") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).stencil_ref_value = val[0];
				return CG_TRUE;
			}
			else if(std::string("stencil_mask") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).stencil_mask = val[0];
				return CG_TRUE;
			}
			else if(std::string("stencil_fail") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).stencil_fail = static_cast<StencilOperation>(val[0]);
				return CG_TRUE;
			}
			else if(std::string("stencil_depth_fail") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).stencil_depth_fail = static_cast<StencilOperation>(val[0]);
				return CG_TRUE;
			}
			else if(std::string("stencil_pass") ==  name)
			{
				const int* val =  cgGetIntStateAssignmentValues( as, &nvalues );
				CGStateManager::GetSingleton().GetPassData(pass).stencil_pass = static_cast<StencilOperation>(val[0]);
				return CG_TRUE;
			}
//texture stages
			else if(std::string("texture") ==  name)
			{
				CGparameter tex = cgGetTextureStateAssignmentValue(as);
				const char* name = cgGetParameterName(tex);
				int index = cgGetStateAssignmentIndex( as );
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].name = name;
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("tex_address_mode") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const int* val = cgGetIntStateAssignmentValues(as,&nvalues);
				if(index < 8)
				{
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].address_mode.u = static_cast<TextureAddressingMode>(val[0]);
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].address_mode.v = static_cast<TextureAddressingMode>(val[1]);
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].address_mode.w = static_cast<TextureAddressingMode>(val[2]);
				}
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("tex_border_color") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const float* val = cgGetFloatStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].border_color = val[0];
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("filtering") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const int* val = cgGetIntStateAssignmentValues(as,&nvalues);
				if(index < 8)
				{
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].min_filter = static_cast<FilterOptions>(val[0]);
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].mag_filter = static_cast<FilterOptions>(val[1]);
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].mip_filter = static_cast<FilterOptions>(val[2]);
				}
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("max_anisotropy") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const int* val = cgGetIntStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].anisotropy = val[0];
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("mipmap_bias") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const float* val = cgGetFloatStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].bias = val[0];
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("transform") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const float* val = cgGetFloatStateAssignmentValues(as,&nvalues);
				if(index < 8)
				{
					Matrix4<REAL> m(val[0],val[1],val[2],val[3],val[4],val[5],val[6],val[7],val[8],val[9],val[10],val[11],val[12],val[13],val[14],val[15]);
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].transform = m;
				}
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("transform_dim") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const int* val = cgGetIntStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].tex_transform_coord_dim = static_cast<TexCoordDim>(val[0]);
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("tex_coord_gen") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const int* val = cgGetIntStateAssignmentValues(as,&nvalues);
				if(index < 8)
				{
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].tex_coord_calc = static_cast<TexCoordCalcMethod>(val[0]);
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].tex_coord_calc_index = val[1];
				}
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("color_blend_operation") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const int* val = cgGetIntStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].color_blend_mode.operation = static_cast<LayerBlendOperationEx>(val[0]);
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("color_blend_sources") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const int* val = cgGetIntStateAssignmentValues(as,&nvalues);
				if(index < 8)
				{
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].color_blend_mode.source1 = static_cast<LayerBlendSource>(val[0]);
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].color_blend_mode.source2 = static_cast<LayerBlendSource>(val[1]);
				}
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("color_blend_factor") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const float* val = cgGetFloatStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].color_blend_mode.factor = val[0];
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("color_blend_color1") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const float* val = cgGetFloatStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].color_blend_mode.colorArg1 = Color(val[0],val[1],val[2],val[3]);
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("color_blend_color2") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const float* val = cgGetFloatStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].color_blend_mode.colorArg2 = Color(val[0],val[1],val[2],val[3]);
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("alpha_blend_operation") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const int* val = cgGetIntStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].alpha_blend_mode.operation = static_cast<LayerBlendOperationEx>(val[0]);
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("alpha_blend_sources") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const int* val = cgGetIntStateAssignmentValues(as,&nvalues);
				if(index < 8)
				{
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].alpha_blend_mode.source1 = static_cast<LayerBlendSource>(val[0]);
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].alpha_blend_mode.source2 = static_cast<LayerBlendSource>(val[1]);
				}
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("alpha_blend_factor") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const float* val = cgGetFloatStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].alpha_blend_mode.factor = val[0];
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("alpha_blend_color1") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const float* val = cgGetFloatStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].alpha_blend_mode.alphaArg1 = Color(val[0],val[1],val[2],val[3]);
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
			else if(std::string("alpha_blend_color2") == name)
			{
				int index = cgGetStateAssignmentIndex( as );
				const float* val = cgGetFloatStateAssignmentValues(as,&nvalues);
				if(index < 8)
					CGStateManager::GetSingleton().GetPassData(pass).texture[index].alpha_blend_mode.alphaArg2 = Color(val[0],val[1],val[2],val[3]);
				else
					throw std::invalid_argument(std::string("CGMaterial: wrong index for texture ") + name);
			}
		}
		return CG_FALSE;
	}
	/*******************************************************************************/
	CGbool ResetStateAssignment(CGstateassignment)
	{
		return CG_TRUE;
	}
	/*******************************************************************************/
	CGbool ValidateStateAssignment(CGstateassignment as)
	{
		//Check only shaders.
		CGstate state = cgGetStateAssignmentState( as );
		if(cgIsState(state))
		{
			const char* name = cgGetStateName( state );
			if(std::string("VertexProgram") == name)
			{
				//CGprogram prog = cgGetProgramStateAssignmentValue( as );
				//const char* compprog = cgGetProgramString( prog, CG_COMPILED_PROGRAM );
				return CG_TRUE;
			}
			else if(std::string("PixelProgram") == name)
			{

				//CGprogram prog = cgGetProgramStateAssignmentValue( as );
				//const char* compprog = cgGetProgramString( prog, CG_COMPILED_PROGRAM );
				//CGparameter param = cgGetFirstParameter(prog,CG_GLOBAL);
				//while ( param )
				//{
				//	const char* pname =  cgGetParameterName( param );
				//	CGtype type = cgGetParameterType( param );
				//	if(type == CG_SAMPLER2D)
				//	{
				//		CGpass pass = cgGetStateAssignmentPass( as );
				//		CGtechnique tech = cgGetPassTechnique( pass );
				//		CGeffect eff = cgGetTechniqueEffect( tech );
				//		CGparameter p = cgGetNamedEffectParameter(eff, pname);
				//		CGstateassignment sa = cgGetFirstSamplerStateAssignment(p);
				//		while(sa)
				//		{
				//			CGstate sastate = cgGetSamplerStateAssignmentState( sa );
				//			const char* name = cgGetStateName( sastate );
				//			cgCallStateValidateCallback(sa);
				//			sa = cgGetNextStateAssignment( sa );
				//		}
				//	}
				//
				//	param = cgGetNextParameter( param );
				//}
				return CG_TRUE;
			}
			return CG_TRUE;
		}
		return CG_FALSE;
	}
	/*******************************************************************************/
}
